package com.library.admin.config;

import com.library.admin.modules.book.service.BookService;
import com.library.admin.modules.menu.service.MenuService;
import com.library.admin.modules.user.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 启动数据加载内存统一管理
 *
 * @author: xyh
 * @create: 2023-10-04
 **/
@Log4j2
@Component
public class InitDataApplication implements ApplicationRunner {

    @Resource
    private UserService userService;
    @Resource
    private MenuService menuService;
    @Resource
    private BookService bookService;
    private boolean initialized = false;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (!initialized) {
            init();
            initialized = true;
        }
    }

    /**
     * 初始化数据
     */
    private void init() {
        //用户缓存初始化
        userService.init();
        //菜单初始化
        menuService.init();
        //图书初始化
        bookService.init();
    }

}
